﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace IntellectShop.Common
{
	public partial class Cart
	{
		public void AddToCart(Common.Commodity commodity)
		{
			using (DB.IntellectShopDataContext storeDB = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var cartItem = storeDB.CartItems
					.FirstOrDefault(
						x => x.CartOwnerID == this.CartOwnerID &&
						x.CommodityID == commodity.CommodityID);
				if (cartItem == null)
				{
					// Create a new cart item
					storeDB.CartItems.InsertOnSubmit(
						new DB.CartItem
						{
							CartOwnerID = this.CartOwnerID,
							CommodityID = commodity.CommodityID,
							Count = 1,
							Created = DateTime.Now
						}
					);
				}
				else
				{
					// Add one to the quantity
					cartItem.Count++;
				}

				// Save it
				storeDB.SubmitChanges();
			}
		}

		public int RemoveFromCart(int cartItemID)
		{
			using (DB.IntellectShopDataContext storeDB = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				int remainingItemsCount = 0;
				//Get the cart
				var cartItem = storeDB.CartItems.FirstOrDefault(cart => cart.CartOwnerID == this.CartOwnerID && cart.CartItemID == cartItemID);
				if (cartItem != null)
				{
					if (cartItem.Count > 1)
					{
						cartItem.Count--;
						remainingItemsCount = cartItem.Count;
					}
					else
					{
						storeDB.CartItems.DeleteOnSubmit(cartItem);
						remainingItemsCount = 0;
					}

					storeDB.SubmitChanges();
				}

				return remainingItemsCount;
			}
		}

		public void EmptyCart(DB.IntellectShopDataContext storeDB)
		{
			var cartItems = storeDB.CartItems.Where(cart => cart.CartOwnerID == this.CartOwnerID);
			foreach (var cartItem in cartItems)
			{
				storeDB.CartItems.DeleteOnSubmit(cartItem);
			}

			storeDB.SubmitChanges();
		}

		public List<Common.CartItem> GetCartItems()
		{
			using (DB.IntellectShopDataContext storeDB = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				List<Common.CartItem> list = new List<CartItem>(); 
				var cartItems =
					from cart in storeDB.CartItems
					where cart.CartOwnerID == this.CartOwnerID
					select cart;
				if (cartItems != null)
				{
					foreach (DB.CartItem item in cartItems.ToList())
					{
						list.Add(new Common.CartItem
						{
							CartItemID = item.CartItemID,
							Commodity = Common.Commodity.RetrieveByID(item.CommodityID),
							Count = item.Count,
							Created = item.Created
						});
					}
				}

				return list;
			}
		}

		public int GetCount()
		{
			using (DB.IntellectShopDataContext storeDB = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				int? count = (
					from cartItems in storeDB.CartItems
					where cartItems.CartOwnerID == this.CartOwnerID
					select (int?)cartItems.Count).Sum();
				return count ?? 0;
			}
		}

		public Common.Money GetTotal()
		{
			using (DB.IntellectShopDataContext storeDB = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				decimal? total = (
					from cartItems in storeDB.CartItems
					where cartItems.CartOwnerID == this.CartOwnerID
					select (int?)cartItems.Count * cartItems.Commodity.Price).Sum();

				return new Common.Money { Amount = total ?? decimal.Zero, Currency = Currency.RUB };
			}
		}

		// When a user has logged in, migrate their shopping cart to
		// be associated with their username
		public void MigrateCart(string userName)
		{
			using (DB.IntellectShopDataContext storeDB = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var shoppingCart = storeDB.CartItems.Where(c => c.CartOwnerID == this.CartOwnerID);
				foreach (DB.CartItem item in shoppingCart)
				{
					item.CartOwnerID = userName;
				}

				storeDB.SubmitChanges();
			}
		}
	}
}
